<template>
	<view class="add page bgf7 p12">
		<view class="bgff p22">
			
			
			<view class="flex flexC borb py12">
				<text class="flex1">设备名字</text>
				<input v-model="name" class=" aliR py3" placeholder="请输入设备名字" />
			
			</view>
			
			<view v-for="(item,index) in list" :key="index" class="flex flexC mb16 mt14">
				<view class="flex flexC flex1">
					<input v-model="item.hour" type="digit" class="flex1 bor r40 px12 py6" placeholder="请输入时间" />
					<text class="ml8">小时</text>
				</view>
				<view class="flex flexC flex1 ">
					<input type="digit" v-model="item.amount" class="flex1 bor r40 px12 py6" placeholder="请输入套餐价格" />
					<text class="ml8">元</text>
					<image @click="delItem(item)" class="w45" src="../../static/img/icon/del_icon.png"></image>

				</view>

			</view>
			<view class="flex flexC aliC mt36">
				<view class="mr30 flex1 c00 bgf6 r40 py12" @click="addItem">添加套餐项</view>
				<view class="flex1 bg41 r40 py12" @click="save">保存套餐数据</view>
			</view>
		</view>
	</view>
</template>

<script>
	export default {
		data() {
			return {

				device_no: '',
				list: [],
				name:''
			};
		},
		onLoad(option) {
			this.device_no = option.device_no;
			this.getLineSetList();
		},

		methods: {


			getLineSetList() {
				let param = {
					device_no: this.device_no,

				};
				let that = this;
				this.$api.getLineSet(param).then(res => {

					if (res.code == 1) {
						this.list = res.data.list;
						this.name = res.data.name;
						if(this.list.length == 0){
							this.addItem();
						}
					} else {

					}
				})
			},


			delItem(item) {
				let param = {
					id: item.id,

				};
				let that = this;
				this.$api.removeLineSet(param).then(res => {

					if (res.code == 1) {
						that.getLineSetList();
					} else {
						uni.showToast({
							title: res.msg,
							icon: 'none'
						})
					}
				})

			},

			addItem() {

				var obj = new Object();
				obj.id = -1;
				obj.hour = "",
				obj.amount = "";
				this.list.push(obj);
			},
			
			save(){
				var isOk = true;
				for(var i=0;i<this.list.length;i++){
					
					if(this.list[i].hour == 0 || this.list[i].amount == 0){
						isOk = false;
					}
				}
				
				if(!isOk){
					uni.showToast({
						title: "时间或者价格不能填0",
						icon: 'none'
					})
					return;
				}
				
				if(this.name == ''){
					uni.showToast({
						title: "名字不能为空",
						icon: 'none'
					})
					return;
				}
				
				let param = {
					device_no: this.device_no,
					name:this.name,
					set:JSON.stringify(this.list)	
				};
				let that = this;
				
				this.$api.addLineSet(param).then(res => {
				
					if (res.code == 1) {
						uni.showModal({
							title:"添加提示",
							content:"添加成功",
							showCancel:false,
							complete() {
								uni.navigateBack({
									delta:1
								})
							}
						})
						
					} else {
						uni.showToast({
							title: res.msg,
							icon: 'none'
						})
					}
				})
				
			}
		}

	}
</script>

<style lang="scss">
	.w45 {
		width: 60rpx;
		height: 60rpx;
	}
</style>
